﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.Events;

public class BirdController : MonoBehaviour {

	CharacterController cc;
	BirdAnimController m_Animator;
	Rigidbody2D rb;
	BirdMoveController bmc;

	public Transform m_BoyFoothold;
	public Transform m_BoyCrouchPoint;
	public LayerMask layerMask;

	public Transform m_SurroundBoyPoint;
	public float m_SurroundBoyRaduis;
	public float m_SorroundTime;
	float m_SorroundTimer;

	float m_Movement;
	bool m_Crouching;
	bool m_StopInShoulder;
	bool m_Fllowing;

	[SerializeField] BirdUnit birdUnit;

	private void Awake() {
		m_StopInShoulder = true;
		rb = gameObject.GetComponent<Rigidbody2D>();
		bmc = gameObject.GetComponent<BirdMoveController>();
		cc = gameObject.GetComponent<CharacterController>();
		m_Animator = gameObject.GetComponent<BirdAnimController>();
	}

	private void Start() {

	}


	public void Move(float move, bool crouch, bool running) {

		if (!BirdState.FollowBoy(bmc.state))
			return;

		OnLanding();

		if (!crouch && m_Crouching || crouch) {
			crouch = true;
			m_StopInShoulder = false;
		}

		if (m_StopInShoulder) {

			if (Mathf.Abs(move) < 2 && !crouch) {
				StopInShoulder(move);
			} else {
				StartCoroutine(SurroundOnBoy());
			}
			return;
		}

		m_Movement = move;

		cc.Move(move, running, crouch, false, false, 0, false);

		if (running && !crouch) {
			m_Animator.setMove(2);
			m_Movement = move * 2;
		} else if (m_Movement == 0) {
			m_Animator.setMove(0);
		} else {
			m_Animator.setMove(1);
		}
		if (!m_Crouching) {
			if (!crouch)
				OnBoyCrouching(false);
			else
				OnBoyCrouching(true);
		} else {
			OnBoyCrouching(true);
		}
	}

	public void StopInShoulder(float move) {
		if (rb.gravityScale != 0)
			rb.gravityScale = 0;

		if (Vector2.Distance(transform.position, m_BoyFoothold.position) < .2f) {
			rb.position = m_BoyFoothold.position;
			m_Animator.setMove(0);
			KeepLookAtBoy();
			m_Fllowing = false;
		} else {
			m_Animator.setMove(2);
			birdUnit.FllowBoy(m_BoyFoothold);
			m_Fllowing = true;
		}

	}
	/// <summary>
	///	小男孩crouch，小鸟对应行为判定
	/// </summary>
	/// <param name="crouch"></param>
	public void OnBoyCrouching(bool crouch) {

		if (!BirdState.FollowBoy(bmc.state))
			return;

		m_Crouching = crouch;

		if (cc != null && cc.Grounded && crouch) {
			return;
		}
		//避免后面的平台是空的，小鸟掉下去
		if(crouch) {
			if(transform.position.y < m_BoyCrouchPoint.position.y) {

				rb.AddForce(- rb.mass * rb.gravityScale * Physics2D.gravity * 2f);
			}
		}

		if (crouch) {

			if ((transform.localScale.x > 0 && (transform.position.x < m_BoyCrouchPoint.position.x || transform.position.x - m_BoyFoothold.position.x >= .3f)) || (transform.localScale.x < 0 && (transform.position.x > m_BoyCrouchPoint.position.x || transform.position.x - m_BoyFoothold.position.x <= -0.3f))) {
				rb.gravityScale = 0f;
				birdUnit.FllowBoy(m_BoyCrouchPoint);
				m_Animator.setMove(2);
			} else {

				Debug.Log(gameObject.name + " : fly to crouch point");
				if (Vector3.Distance(transform.position, m_BoyFoothold.position) == 0 || (transform.position.x - m_BoyCrouchPoint.position.x) * transform.localScale.x >= 0) {
					rb.AddForce(Vector2.left * 30 * transform.localScale.x);
					m_Animator.setMove(2);
				} else if ((transform.position.x - m_BoyCrouchPoint.position.x) * transform.localScale.x <= 0) {
					m_Animator.setMove(2);
				}
			}

		} else {
			StartCoroutine(SurroundOnBoy());
		}
	}

	/// <summary>
	/// 在规定时间内 脱离主角 飞行
	/// 
	/// </summary>
	/// <returns></returns>
	IEnumerator SurroundOnBoy() {

		m_SorroundTimer += Time.deltaTime;
		if (m_SorroundTimer >= m_SorroundTime) {
			m_StopInShoulder = true;
			m_SorroundTimer = 0;
		}

		if (Physics2D.OverlapCircle(m_SurroundBoyPoint.position, m_SurroundBoyRaduis, layerMask) != null && m_Movement == 0) {
			Vector2 force = Vector2.zero;

			if (transform.position.x >= m_SurroundBoyPoint.position.x + m_SurroundBoyRaduis)
				force = Vector2.left;

			rb.AddForce(force * 10);
			m_Animator.setMove(2);

			//yield return new WaitForSeconds(2);
		} else {

			rb.velocity = m_SurroundBoyPoint.position - transform.position;

			m_Animator.setMove(2);
			yield return new WaitForSeconds(1);

		}

		yield return null;

	}

	public void OnBoyPushing(bool push) {
		if (push) {
			StopCoroutine(SurroundOnBoy());
			StartCoroutine(SurroundOnBoy());
		}
	}

	public void KeepLookAtBoy() {
		float face = m_BoyFoothold.parent.position.x - transform.position.x > 0 ? 1 : -1;
		cc.Flip(face);
	}

	void OnLanding() {
		if (cc.Grounded) {
			KeepLookAtBoy();
			rb.gravityScale = 3f;
		}
		else
			rb.gravityScale = .3f;

	}

	private void OnDrawGizmos() {


		Gizmos.color = Color.red;
		if (m_SurroundBoyPoint != null) {
			Gizmos.DrawWireSphere(m_SurroundBoyPoint.position, m_SurroundBoyRaduis);

		}

		if (m_BoyCrouchPoint != null) {
			Gizmos.DrawLine(m_BoyCrouchPoint.position, m_BoyCrouchPoint.position + Vector3.up / 2);
		}


	}


}
